package com.shaoshaohuo.app.ui;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PointMall;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.utils.ImageLoadUtil;
import com.shaoshaohuo.app.utils.StringHelper;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class PointExchangeEditActivity extends BaseActivity {

	private TopbarView mTopbarView;

	private ImageView imageView;
	private TextView mGoodsTitleText;
	private TextView mPointText;
	
	private EditText mCountEdit;
	private EditText mNameEdit;
	private EditText mPhoneEdit;
	private EditText mAddressEdit;
	private Button mNextButton;

	private PointMall pointGoods;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_point_exchange_edit);
		initData();
		initView();
		setUpView();
	}

	private void initData() {
		this.pointGoods = (PointMall) getIntent().getSerializableExtra(ExtraName.pointGoods);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		
		imageView = (ImageView) this.findViewById(R.id.imageview_car_user_headview);
		mGoodsTitleText = (TextView) this.findViewById(R.id.textview_car_username);
		mPointText = (TextView) this.findViewById(R.id.textview_car_phone);

		mCountEdit = (EditText) this.findViewById(R.id.edittext_count);
		mNameEdit = (EditText) this.findViewById(R.id.edittext_name);
		mPhoneEdit = (EditText) this.findViewById(R.id.edittext_phone);
		mAddressEdit = (EditText) this.findViewById(R.id.edittext_mark);
		
		mNextButton = (Button) this.findViewById(R.id.button_next);
	}

	private void setUpView() {
		mTopbarView.setCenterText("兑换");
		mTopbarView.setLeftView(true, true);

//		LoadUtils.getInstance().loadImage(imageView, pointGoods.getImg());
		ImageLoader.getInstance().displayImage(pointGoods.getImg(), imageView,ImageLoadUtil.build);
		mGoodsTitleText.setText(pointGoods.getTitle());
		mPointText.setText(pointGoods.getIntegral()+"积分");
		
		mNextButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				exchange();
			}
		});
	}

	protected void exchange() {
		
		String num = mCountEdit.getText().toString().trim();
		String contact = mNameEdit.getText().toString().trim();
		String phone = mPhoneEdit.getText().toString().trim();
		String address = mAddressEdit.getText().toString().trim();
		
		if(TextUtils.isEmpty(num)){
			showToast("请输入兑换数量");
			return;
		}
		if(TextUtils.isEmpty(contact)){
			showToast("请输入收货人姓名");
			return;
		}
		if(TextUtils.isEmpty(phone)){
			showToast("请输入联系电话");
			return;
		}
		if(TextUtils.isEmpty(address)){
			showToast("请输入收货地址");
			return;
		}
		
		if(Integer.parseInt(num)<=0){
			showToast("兑换数量应大于0");
			return;
		}
		
		if(!"-1".equals(pointGoods.getFreenum()) && Integer.parseInt(num)>Integer.parseInt(pointGoods.getFreenum())){
			showToast("兑换商品数量不足");
			return;
		}
		
		if(!StringHelper.isPhoneNumber(phone)){
			showToast("请输入合格的电话号码");
			return;
		}
		
		startLoadingDialog();
		RequestService.getInstance().exchangeGoods(this,pointGoods.getGoodsid() , num, contact, phone, address, BaseEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					showToast("兑换成功");
					Intent intent = new Intent(PointExchangeEditActivity.this,MyPointsActivity.class);
					startActivity(intent);
					finish();
				}else{
					showToast(resultData.getMsg());
				}
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
	}
	
}
